{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nation</th>\n",
       "      <th>capital</th>\n",
       "      <th>GDP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Janpan</td>\n",
       "      <td>Tokyo</td>\n",
       "      <td>4000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>S.Korea</td>\n",
       "      <td>Seoul</td>\n",
       "      <td>1300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>China</td>\n",
       "      <td>Beijing</td>\n",
       "      <td>9100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    nation  capital   GDP\n",
       "0   Janpan    Tokyo  4000\n",
       "1  S.Korea    Seoul  1300\n",
       "2    China  Beijing  9100"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "dataNumPy32=np.asarray([('Janpan','Tokyo',4000),('S.Korea','Seoul',1300),('China','Beijing',9100)])\n",
    "DF32=pd.DataFrame(dataNumPy32,columns=['nation','capital','GDP'])\n",
    "DF32"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "map:以相同规则将一列数据做一个映射，也就是进行相同函数的处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nation</th>\n",
       "      <th>capital</th>\n",
       "      <th>GDP</th>\n",
       "      <th>GDP_Level</th>\n",
       "      <th>NATION</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Janpan</td>\n",
       "      <td>Tokyo</td>\n",
       "      <td>4000</td>\n",
       "      <td>Medium</td>\n",
       "      <td>JANPAN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>S.Korea</td>\n",
       "      <td>Seoul</td>\n",
       "      <td>1300</td>\n",
       "      <td>low</td>\n",
       "      <td>S.KOREA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>China</td>\n",
       "      <td>Beijing</td>\n",
       "      <td>9100</td>\n",
       "      <td>High</td>\n",
       "      <td>CHINA</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    nation  capital   GDP GDP_Level   NATION\n",
       "0   Janpan    Tokyo  4000    Medium   JANPAN\n",
       "1  S.Korea    Seoul  1300       low  S.KOREA\n",
       "2    China  Beijing  9100      High    CHINA"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def GDP_Factorize(v):\n",
    "    fv=np.float64(v)\n",
    "    if fv > 6000.0:\n",
    "        return 'High'\n",
    "    elif fv < 2000.0:\n",
    "        return 'low'\n",
    "    else:\n",
    "        return 'Medium'\n",
    "DF32['GDP_Level'] = DF32['GDP'].map(GDP_Factorize)\n",
    "DF32['NATION'] = DF32.nation.map(str.upper)\n",
    "DF32"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "applymap功能类似于map，可以对dataframe里面的每一个元素像map那样全局操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nation</th>\n",
       "      <th>capital</th>\n",
       "      <th>GDP</th>\n",
       "      <th>GDP_Level</th>\n",
       "      <th>NATION</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "      <td>8000</td>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "      <td>2600</td>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "      <td>18200</td>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "      <td>&lt;built-in method upper of str object at 0x0000...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                              nation  \\\n",
       "0  <built-in method upper of str object at 0x0000...   \n",
       "1  <built-in method upper of str object at 0x0000...   \n",
       "2  <built-in method upper of str object at 0x0000...   \n",
       "\n",
       "                                             capital    GDP  \\\n",
       "0  <built-in method upper of str object at 0x0000...   8000   \n",
       "1  <built-in method upper of str object at 0x0000...   2600   \n",
       "2  <built-in method upper of str object at 0x0000...  18200   \n",
       "\n",
       "                                           GDP_Level  \\\n",
       "0  <built-in method upper of str object at 0x0000...   \n",
       "1  <built-in method upper of str object at 0x0000...   \n",
       "2  <built-in method upper of str object at 0x0000...   \n",
       "\n",
       "                                              NATION  \n",
       "0  <built-in method upper of str object at 0x0000...  \n",
       "1  <built-in method upper of str object at 0x0000...  \n",
       "2  <built-in method upper of str object at 0x0000...  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DF32.applymap(lambda x: float(x)*2 if x.isdigit() else x.upper)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
